package com.zytool.easyrestful.core.advice;

import com.zytool.easyrestful.core.bean.ResultBean;
import com.zytool.easyrestful.core.exception.BizException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class ExceptionAdvice {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object errorHandler(HttpServletRequest request, HttpServletResponse resp, Exception e) {
        ResultBean result = new ResultBean();
        if (e instanceof BizException) {
            BizException except = (BizException) e;
            result.setCode(except.getCode());
            result.setMessage(except.getMessage());
        } else {
            result.setCode(999);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
